#!/usr/bin/python
# -*- coding: UTF-8 -*-

import time 
from MiniBotControlLib import MiniBotControlLib
from BwSensorLib import BwSensorLib

# 声明控制库对象
mlib = MiniBotControlLib("udp")
# 声明传感器库对象
sensorlib = BwSensorLib(mlib)
# 连接机器人
ip = "10.10.100.254"
mlib.Connect(ip, 8899, 2000)


# 巡线传感器检测并根据检测结果显示不同表情
def main():
   # 点阵显示表情
   # 对应的表情分别是：睁大眼睛、委屈、鄙视、无语、惊喜、眨眼
   displayData = [[0x36,0x49,0x49,0x00,0x3E],[0x63,0x14,0x22,0x00,0x3E],[0x77,0x44,0x00,0x2A,0x14],
               [0x41,0x36,0x00,0x1C,0x22],[0x22,0x77,0x22,0x1C,0x14],[0x20,0x12,0x20,0x08,0x1C]]
   while 1 :
      tracking = sensorlib.readTrackingSensor() # 读取巡线传感器数据
      time.sleep(0.1)
      # ir1检测到黑线，ir2检测到黑线
      if (tracking[0] == 0 and tracking[1] == 0):
         sensorlib.setMatrixSensor(displayData[0]) # 显示睁大眼睛
      # ir1检测到黑线，ir2检测到非黑线
      elif (tracking[0] == 0 and tracking[1] == 1):
         sensorlib.setMatrixSensor(displayData[1]) # 显示委屈
      # ir1检测到非黑线，ir2检测到黑线   
      elif (tracking[0] == 1 and tracking[1] == 0):
         sensorlib.setMatrixSensor(displayData[2]) # 显示鄙视
      # ir1检测到非黑线，ir2检测到非黑线
      elif (tracking[0] == 1 and tracking[1] == 1):
         sensorlib.setMatrixSensor(displayData[3])  # 显示无语    

if __name__ == "__main__":
    try:
        main()
    except KeyboardInterrupt:
        print(" Program closed! ")
    pass
